package com.zwb.boot.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zwb.boot.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author 头发又黑又长
 * @Date 2022/7/15 21:52
 * @email zwb15083976291@163.com
 */
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Autowired
    private ObjectMapper jsonMapper;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException, IOException {
        System.out.println(accessDeniedException);
        HashMap<String, Object> result = new HashMap<>();
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(jsonMapper.writeValueAsString(R.error("拒绝访问")));
    }
}